<?php
namespace app\api\controller\v1;
use app\api\controller\v1\Init;

use think\Validate;

class Sms extends Init
{
    protected $model = null;

	public function _initialize() {
	    parent::_initialize();

        $this->model = model('admin/dysms', 'service');
    }
	/* 发送验证码 */
	public function send(){
		$mobile = $this->params['mobile'];
		$content = $this->params['content'];
        $scene = $this->params['scene'];

        $rule = [
            'mobile'	=> 'regex:/^1[34578]{1}[0-9]{9}$/',
        ];
        $msg = [
            'mobile.regex'	=> '手机号格式错误',
        ];
        $data = [
            'mobile'	=> $mobile,
        ];
        $validate = new Validate($rule, $msg);
        $check_mobile = $validate->check($data);
        if (!$check_mobile) {
            return $this->response($validate->getError(),false,-30001);
        }
        $result = $this->model->send($mobile, $content, '', $scene);
		if (!$result) {
            return $this->response($this->model->getError(),false,-30002);
        }
        return $this->response(__('短信发送成功，请查收'));
	}


    /**
     * 短信验证
     * @return \think\Response
     */
	public function check() {
        $rule = [
            'mobile'	=> 'regex:/^1[34578]{1}[0-9]{9}$/',
        ];
        $msg = [
            'mobile.regex'	=> '手机号格式错误',
        ];
        $data = [
            'mobile'	=> $this->params['mobile'],
        ];
        $validate = new Validate($rule, $msg);
        if (!$validate->check($data)) {
            return $this->response($validate->getError(),false,-30001);
        }

        $destroy = (isset($this->params['destroy'])) ? true : false;
        $result = $this->model->check($this->params['mobile'], $this->params['code'] ,$this->params['scene'], $destroy);
        if (!$result) {
            return $this->response($this->model->getError(),false,-30002);
        }
        return $this->response(__('操作成功'));
    }

    public function test() {
	    $result = model('wechat/template_message', 'service')->send();
	    $this->response('', $result);
    }

}
